/**
 *	@file	mempool.h
 *
 *	@brief	mempool is a pool for malloc arbitrary memory size, the alloced
 *		memory not need release after use, it'll released when the pool
 *		destroy.
 *
 *	@date	2009-03-16
 *
 *	@author	Forrest.zhang
 */

#ifndef FZ_MEMPOOL_H
#define FZ_MEMPOOL_H

#include <pthread.h>
#include <sys/types.h>

/* memory pool data */
typedef struct mempool_data {
	u_int8_t	*last;
	u_int8_t	*end;
	mempool_t	*next;
	u_int32_t	failed;
} mempool_data_t;

/* large memory pool */
typedef struct mempool_large {
	struct mempool_large	*next;
	void		*alloc;
} mempool_large_t;

/* memory pool */
typedef struct mempool {
	u_int32_t	magic;	/* magic number for verify */
	u_int32_t	size;	/* the pool size */
	u_int32_t	max;	/* the max alloced size */
	struct mempool	*current;/* the current pool */
	mempool_large_t *large;	/* the large memory list */
} mempool_t;

/**
 *	Alloc a new memory pool, the pool size is @size.
 *
 *	Return non-void pointer if success, NULL means error
 */
extern mempool_t*
mempool_create(size_t size);

/**
 *	Free memory pool @pool when it's not used. All memory 
 *	in it will free.
 *
 *	No return value
 */
extern void 
mempool_destroy(mempool_t *pool);

/**
 *	Get a free memory from memory pool @pool, the returned
 *	memory size is @size. 
 *	
 *	Return non-void pointer if success, NULL means error.
 */
extern void *
mempool_alloc(mempool_t *pool, size_t size);

/**
 *	Get a free memory from mempool_t @pool, the returned
 *	memory size is @size and is zeroed.
 *	
 *	Return non-void pointer if success, NULL means error.
 */
extern void *
mempool_zalloc(mempool_t *pool, size_t size);

/**
 *	Print the memory pool @pool in stdout.
 *
 *	No return value
 */
extern void 
mempool_print(mempool_t *pool);


#endif /* end of FZ_MEMPOOL_H */


